#region Using Statements
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
#endregion

//Method Documentation
// Battle Algorithm WORK IN PROGRESS
//  BattleAlgorithm(Damage, Bonus Damage from Items)

// Draw Menu
// Draws a menu using one simple method. How to use it?
//  DrawMenu(MenuTexture, Screen that Menu is to be
//                  displayed on (ie: Screen 11 (Battle Screen)))
// IE: DrawMenu(BattleScreen, 11)

// Draw Selector
// Draws the selector item. Only DRAWS it. Texture definition and
//   Vectors are still required. Only simplifies the code and makes it less messy.
// How to:
// DrawSelector(Selector Texture, Selector Position (See Update Area), Screen on which
//      Selector is to be displayed on.

namespace LlamaFarmV1._
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region 2D Textures
        Texture2D StartTexture;
        Texture2D GameSelectScreen;
        Texture2D HelpScreen;
        Texture2D Load;
        
        Texture2D Select;
        Texture2D LlamaSelect;
        Texture2D Story;
        Texture2D Credits;


        //Llamas
        Texture2D Green;
        Texture2D White;
        Texture2D Yellow;

        Texture2D Grey;
        Texture2D Red;
        Texture2D Blue;
        Texture2D Tiger;
        Texture2D Rainbow;
        Texture2D Pink;
        Texture2D DGreen;
        Texture2D Zebra;
        Texture2D Camo;
        Texture2D Racing;
        Texture2D Black;
        Texture2D Dalai;

        //Game Select Items
        Texture2D Select2;
        Texture2D Save;
        Texture2D ShopChoose;
        Texture2D InventChoose;
        Texture2D BattleChoose;

        Texture2D BattleScreen;
        Texture2D BattleSelector;

        Texture2D WinBattle1;
        Texture2D WinBattle2;

        Texture2D Results;

        Texture2D Shop;
        Texture2D ShopArrow;

        Texture2D BottomBar;

        Texture2D LlamaTexture;
        Texture2D EnemyLlamaTexture;
        #endregion

        #region Define Vectors
        //Game Select Vectors
        Vector2 GSS0;
        Vector2 GSS1;
        Vector2 GSS2;
        Vector2 GSS3;
        Vector2 GSS4;
        Vector2 SelectGo;
        //Llama Select Vectors
        Vector2 LLS0;
        Vector2 LLS1;
        Vector2 LLS2;
        Vector2 LLS3;
        Vector2 LlamaSelectGo = Vector2.Zero;
        //Select 2 Vectors
        Vector2 S0;
        Vector2 S1;
        Vector2 S2;
        Vector2 S3;
        Vector2 S4;
        Vector2 SSG;
        //BattleSelectVectors
        Vector2 BS0;
        Vector2 BS1;
        Vector2 BS2;
        Vector2 BS3;
        Vector2 BSG;
        //ShopScreenVectors
        Vector2 SS0;
        Vector2 SS1;
        Vector2 SS2;
        Vector2 SS3;
        Vector2 SS4;
        Vector2 SS5;
        Vector2 SS6;
        Vector2 SS7;
        Vector2 SS8;
        Vector2 SS9;
        Vector2 SS10;
        Vector2 SS11;
        Vector2 SS12;
        Vector2 SS13;
        Vector2 SS14;
        Vector2 SS15;
        Vector2 SS16;
        Vector2 SS17;
        Vector2 SS18;
        Vector2 SS19;
        Vector2 SS20;
        Vector2 SS21;
        Vector2 SS22;
        Vector2 SS23;
        Vector2 SSSG;

        Vector2 GoldPos;
        //Llama Select Screen Llama Pos
        Vector2 LlamaPos;

        //Select Screen Info
        Vector2 SelectInfo;
        #endregion

        #region Debug
        string DebugString;
        int DebugInt;
        float DebugFloat;
        bool DebugBool;
        #endregion

        #region Integers
        #region Screen Related/Selection Stuff
        int Screen = 0;
        int SelectPos = 0;
        int SelectPos2 = 0;
        int LlamaSelectPos = 0;
        int BattleSelectPos = 1;
        int ShopSelectPos = 23;
        #endregion

        #region Player Related
        int Llama = 0;
        int PlayerDamage = 0;

        int CurrentLevel = 0;
        int PlayerHealth = 0;
        int CurrentPlayerHealth = 0;
        int PlayerGold = 0;
        int PlayerEarnedGold = 0;
        #endregion

        #region Enemy Related
        int CurrentEnemyLevel = 0;
        int EnemyNumber = 0;

        int EnemyHealth = 0;
        int CurrentEnemyHealth = 0;
        #endregion

        float battleTime = 0f;
        float resultsTime = 0f;
        float timer = 0f;
        #endregion

        #region Booleans
        bool RunBattleTime = true;
        bool CanAttack = false;
        bool DisplayBottomBar = false;
        bool BattleWin = false;
        bool BattleLose = false;
        bool IsInBattle = false;
        bool RunResultsTime = false;
        bool CanEnter = false;
        bool AIisDone = false;
        bool RunAITime = false;
        #endregion

        #region Misc Shit
        KeyboardState previousKeyboardState;
        KeyboardState keyboardState;

        Rectangle viewportRect;

        GraphicsDeviceManager graphics;
        ContentManager content;

        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        SpriteFont spriteFont2;

        XmlDocument xDoc = new XmlDocument();

        Color FontColor;

        System.Windows.Forms.OpenFileDialog file = new System.Windows.Forms.OpenFileDialog();

        string EnemyLlamaName = string.Empty;
        string PlayerLlamaName = string.Empty;
        string CurrentXML = string.Empty;
        #endregion

        bool ContentLoaded = false;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            Screen = 1;
            Llama = 0;
            LlamaSelectPos = 0;
            SelectPos2 = 0;
            CurrentLevel = 0;

            this.IsMouseVisible = true;

            #region Game Select Vectors
            //Game Screen Select
            GSS0 = new Vector2(275.0f, 100.0f);
            GSS1 = new Vector2(324.0f, 170.0f);
            GSS2 = new Vector2(324.0f, 275.0f);
            GSS3 = new Vector2(324.0f, 380.0f);
            GSS4 = new Vector2(310.0f, 489.0f);
            //Llama Select
            LLS0 = new Vector2(65.0f, 110.0f);
            LLS1 = new Vector2(65.0f, 180.0f);
            LLS2 = new Vector2(65.0f, 315.0f);
            LLS3 = new Vector2(65.0f, 445.0f);
            //Select Screen 2 Select
            S0 = new Vector2(75.0f, 25.0f);
            S1 = new Vector2(75.0f, 70.0f);
            S2 = new Vector2(75.0f, 220.0f);
            S3 = new Vector2(75.0f, 380.0f);
            S4 = new Vector2(75.0f, 535.0f);
            //BattleScreenVectors
            BS0 = new Vector2(334.0f, 498.0f);
            BS1 = new Vector2(334f, 526f);
            BS2 = new Vector2(334f, 554f);
            BS3 = new Vector2(334f, 0f);
            //ShopScreenVectors
            //Exit
            SS0 = new Vector2(735.0f, 305.0f);
            //Swords Start
            SS1 = new Vector2(34.0f, 87.0f);
            SS2 = new Vector2(147.0f, 87.0f);
            SS3 = new Vector2(240.0f, 98.0f);
            SS4 = new Vector2(330.0f, 99.0f);
            SS5 = new Vector2(435.0f, 87.0f);
            SS6 = new Vector2(530.0f, 115.0f);
            SS7 = new Vector2(640.0f, 100.0f);
            SS8 = new Vector2(750.0f, 100.0f);
            //Swords End
            //Bows Start
            SS9 = new Vector2(100.0f, 145.0f);
            SS10 = new Vector2(100.0f, 205.0f);
            SS11 = new Vector2(105.0f, 280.0f);
            SS12 = new Vector2(105.0f, 360.0f);
            SS13 = new Vector2(105.0f, 420.0f);
            //Bows end
            //Armor Start
            SS14 = new Vector2(95.0f, 580.0f);
            SS15 = new Vector2(215.0f, 580.0f);
            SS16 = new Vector2(295.0f, 580.0f);
            SS17 = new Vector2(392.0f, 580.0f);
            SS18 = new Vector2(485.0f, 580.0f);
            //Armor End
            //Potions Start
            SS19 = new Vector2(610.0f, 580.0f);
            SS20 = new Vector2(695.0f, 580.0f);
            SS21 = new Vector2(760f, 580.0f);
            //Potion End
            //Stone of Higher Callings
            SS22 = new Vector2(355.0f, 280.0f);
            //Llama Purchase Screen
            SS23 = new Vector2(395.0f, 420.0f);

            //Llama Draw Vec 01
            LlamaPos = new Vector2(415.0f, 150.0f);

            //Select Info Vec
            SelectInfo = new Vector2(375f, 15f);
            #endregion

            #region Viewport Rectangle
            viewportRect = new Rectangle(0, 0,
            graphics.GraphicsDevice.Viewport.Width,
            graphics.GraphicsDevice.Viewport.Height);
            #endregion

            base.Initialize();
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                Load = content.Load<Texture2D>("Sprites\\Loading");
                spriteFont = content.Load<SpriteFont>("Sprites\\Fonts\\Font");
                ThreadLoad();
            }
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                content.Unload();
            }
        }

        protected override void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            #region Start Screen Keyboard
            if (DetectEnter())
            {
                {
                    if (Screen == 1)
                    {
                        Screen = 2;
                    }
                    else if (Screen == 7)
                    {
                        Screen = 8;
                    }
                    else if (Screen == 3)
                    {
                        SelectPos = 0;
                        Screen = 2;
                    }
                    else if (Screen == 5)
                    {
                        SelectPos = 0;
                        Screen = 2;
                    }
                }
            }
            #endregion

            #region Start Screen
            #region Select Pos Keyboard
            if (DetectKey(Keys.Up) && Screen == 2)
            {
                SelectPos -= 1;
            }

            if (DetectKey(Keys.Down) && Screen == 2)
            {
                SelectPos += 1;
            }

            if (SelectPos > 4)
            {
                SelectPos = 0;
            }
            else if (SelectPos < 0)
            {
                SelectPos = 4;
            }

            #endregion

            #region Screen Select #1
            if (Screen == 2)
            {
                if (DetectEnter())
                {
                    if (SelectPos == 1)
                    {
                        Screen = 6;
                        LlamaSelectPos = 0;
                    }
                    if (SelectPos == 2)
                    {
                        LoadGame();
                    }
                    if (SelectPos == 3)
                    {
                        Screen = 3;
                        LlamaSelectPos = 0;
                    }
                    if (SelectPos == 4)
                    {
                        Screen = 5;
                        LlamaSelectPos = 0;
                    }
                }
            }

            #endregion

            #region Select Position
            if (SelectPos == 1)
            {
                SelectGo = GSS1;
            }
            else if (SelectPos == 2)
            {
                SelectGo = GSS2;
            }
            else if (SelectPos == 3)
            {
                SelectGo = GSS3;
            }
            else if (SelectPos == 4)
            {
                SelectGo = GSS4;
            }
            else if (SelectPos == 0)
            {
                SelectGo = GSS0;
            }
            #endregion
            #endregion

            #region Llama Select Screen
            #region Llama Select Screen
            if (Screen == 6)
            {
                if (DetectKey(Keys.Up))
                {
                    LlamaSelectPos -= 1;
                }

                if (DetectKey(Keys.Down))
                {
                    LlamaSelectPos += 1;
                }

                if (LlamaSelectPos > 3)
                {
                    LlamaSelectPos = 0;
                }
                else if (LlamaSelectPos < 0)
                {
                    LlamaSelectPos = 3;
                }
            }
            #endregion

            #region Llama Select Position
            if (Screen == 6)
            {
                if (LlamaSelectPos == 1)
                {
                    LlamaSelectGo = LLS1;
                }
                else if (LlamaSelectPos == 2)
                {
                    LlamaSelectGo = LLS2;
                }
                else if (LlamaSelectPos == 3)
                {
                    LlamaSelectGo = LLS3;
                }
                else if (LlamaSelectPos == 0)
                {
                    LlamaSelectGo = LLS0;
                }
            }
            #endregion

            #region Llama Select Keyboard
            if (Screen == 6)
            {
                if (DetectEnter())
                {
                    SelectPos = 0;
                    if (LlamaSelectPos == 1)
                    {
                        Llama = 1;
                        Screen = 7;
                        GetLlamaLevel(Llama);
                    }
                    if (LlamaSelectPos == 2)
                    {
                        Llama = 2;
                        Screen = 7;
                        GetLlamaLevel(Llama);
                    }
                    if (LlamaSelectPos == 3)
                    {
                        Llama = 3;
                        Screen = 7;
                        GetLlamaLevel(Llama);
                    }
                    if (LlamaSelectPos == 0)
                    {
                        Llama = 0;
                        Screen = 6;
                        GetLlamaLevel(Llama);
                    }
                }
            }
            #endregion
            #endregion

            #region Select Screen 2

            #region Select Screen 2
            if (Screen == 8 && !IsInBattle)
            {
                DebugInt = 0;
                if (DetectEnter())
                {
                    if (SelectPos2 == 1)
                    {
                        Screen = 9;
                        SelectPos2 = 0;
                    }
                    if (SelectPos2 == 2)
                    {
                        Screen = 10;
                        SelectPos2 = 0;
                    }
                    if (SelectPos2 == 3)
                    {
                        Screen = 11;
                        InitializeBattle(Llama);
                        IsInBattle = true;
                        SelectPos2 = 0;
                    }
                    if (SelectPos2 == 4)
                    {
                        SaveGame();
                    }
                }
            }
            #endregion

            #region Select2 Pos Keyboard
            if (DetectKey(Keys.Up) && Screen == 8)
            {
                SelectPos2 -= 1;
            }

            if (DetectKey(Keys.Down) && Screen == 8)
            {
                SelectPos2 += 1;
            }

            if (SelectPos2 > 4)
            {
                SelectPos2 = 0;
            }
            else if (SelectPos2 < 0)
            {
                SelectPos2 = 4;
            }

            #endregion

            #region Select2 Select Position
            if (SelectPos2 == 1)
            {
                SSG = S1;
            }
            else if (SelectPos2 == 2)
            {
                SSG = S2;
            }
            else if (SelectPos2 == 3)
            {
                SSG = S3;
            }
            else if (SelectPos2 == 4)
            {
                SSG = S4;
            }
            else if (SelectPos2 == 0)
            {
                SSG = S0;
            }
            #endregion

            #endregion

            #region Battle Screen
            #region Battle Screen Pos Keyboard
            if (BattleWin == false && BattleLose == false && Screen == 11)
            {
                if (DetectKey(Keys.Up))
                {
                    BattleSelectPos -= 1;
                }

                if (DetectKey(Keys.Down, 11))
                {
                    BattleSelectPos += 1;
                }

                if (BattleSelectPos > 3)
                {
                    BattleSelectPos = 1;
                }
                else if (BattleSelectPos == 0)
                {
                    BattleSelectPos = 3;
                }
            }

            #endregion

            #region Battle Screen Select Position
            if (BattleSelectPos == 1)
            {
                BSG = BS0;
            }
            else if (BattleSelectPos == 2)
            {
                BSG = BS1;
            }
            else if (BattleSelectPos == 3)
            {
                BSG = BS2;
            }
            #endregion

            #region Battle Select Keyboard
            if (Screen == 11)
            {
                if (DetectEnter() && !BattleWin && !BattleLose)
                {
                    if (BattleSelectPos == 1 && CanAttack)
                    {
                        PlayerDamage = GenerateDamage(GetLlamaDamage(Llama), CurrentEnemyLevel);
                        CurrentEnemyHealth -= PlayerDamage;
                        if (CurrentPlayerHealth >= 0 & CurrentEnemyHealth >= 0 )
                            AI(GetLlamaLevel(EnemyNumber), gameTime);
                    }
                    /*if (LlamaSelectPos == 2)
                    {
                        Screen = 7;
                    }*/
                    if (BattleSelectPos == 3 & Screen == 11)
                    {
                        if (RunAway())
                        {
                            Screen = 8;
                            EndBattle();
                        }
                    }
                }
            }

            if (BattleWin)
            {
                if (DetectEnter() && Screen == 11)
                {
                    GetBattleResults();
                    PlayerEarnedGold = GenerateGold(CurrentEnemyLevel, Llama);
                    PlayerGold += PlayerEarnedGold;
                    EndBattle();
                    Screen = 13;
                }
            }

            if (BattleLose)
            {
                if (DetectEnter() && Screen == 11)
                {
                    GetBattleResults();
                    EndBattle();
                    Screen = 8;
                }
            }

            if (Screen == 13)
            {
                if (DetectEnter() && CanEnter)
                {
                    Screen = 8;
                }
            }

            #endregion

            #region Battle Time
            if (RunBattleTime && Screen == 11)
            {
                battleTime += gameTime.ElapsedGameTime.Milliseconds;
            }

            if (battleTime > 30.0f)
            {
                battleTime = 0;
                RunBattleTime = false;
                CanAttack = true;
            }
            #endregion

            #region Results Time
            if (RunResultsTime && Screen == 13)
            {
                resultsTime += gameTime.ElapsedGameTime.Milliseconds;
            }

            if (resultsTime > 30.0f)
            {
                resultsTime = 0;
                RunResultsTime = false;
                CanEnter = true;
            }
            #endregion

            #region AI time
            int Damage;

            if (RunAITime && Screen == 11)
            {
                timer += gameTime.ElapsedGameTime.Milliseconds;
                DebugFloat = timer;
            }

            if (timer > 300.0f)
            {
                timer = 0;
                RunAITime = false;

                if (!AIisDone)
                {
                    Damage = GenerateDamage(GetLlamaDamage(EnemyNumber), GetLlamaLevel(EnemyNumber));
                    CurrentPlayerHealth -= Damage;
                    DebugInt = Damage;
                }
                AIisDone = true;
            }
            #endregion
            #endregion

            #region Shop Screen
            #region Shop Screen Pos Keyboard
            if (DetectKey(Keys.Up) && Screen == 9)
            {
                ShopSelectPos -= 1;
            }

            if (DetectKey(Keys.Down) && Screen == 9)
            {
                ShopSelectPos += 1;
            }

            if (ShopSelectPos > 24)
            {
                ShopSelectPos = 1;
            }
            else if (ShopSelectPos == 0)
            {
                ShopSelectPos = 24;
            }

            #endregion

            #region Shop Screen Select Position
            if (Screen == 9)
            {
                if (ShopSelectPos == 8) { SSSG = SS1; }
                if (ShopSelectPos == 7) { SSSG = SS2; }
                if (ShopSelectPos == 6) { SSSG = SS3; }
                if (ShopSelectPos == 5) { SSSG = SS4; }
                if (ShopSelectPos == 4) { SSSG = SS5; }
                if (ShopSelectPos == 3) { SSSG = SS6; }
                if (ShopSelectPos == 2) { SSSG = SS7; }
                if (ShopSelectPos == 1) { SSSG = SS8; }
                if (ShopSelectPos == 9) { SSSG = SS9; }
                if (ShopSelectPos == 10) { SSSG = SS10; }
                if (ShopSelectPos == 11) { SSSG = SS11; }
                if (ShopSelectPos == 12) { SSSG = SS12; }
                if (ShopSelectPos == 13) { SSSG = SS13; }
                if (ShopSelectPos == 14) { SSSG = SS14; }
                if (ShopSelectPos == 15) { SSSG = SS15; }
                if (ShopSelectPos == 16) { SSSG = SS16; }
                if (ShopSelectPos == 17) { SSSG = SS17; }
                if (ShopSelectPos == 18) { SSSG = SS18; }
                if (ShopSelectPos == 19) { SSSG = SS19; }
                if (ShopSelectPos == 20) { SSSG = SS20; }
                if (ShopSelectPos == 21) { SSSG = SS21; }
                if (ShopSelectPos == 22) { SSSG = SS22; }
                if (ShopSelectPos == 23) { SSSG = SS23; }
                if (ShopSelectPos == 24) { SSSG = SS0; }
            }
            #endregion

            #region Shop Select Keyboard
            if (Screen == 9)
            {
                if (DetectEnter() && ShopSelectPos == 24)
                {
                    Screen = 8;
                    ShopSelectPos = 23;
                }
            }
            #endregion
            #endregion

            #region BottomBar
            if (keyboardState.IsKeyDown(Keys.LeftShift) | keyboardState.IsKeyDown(Keys.RightShift))
            {
                DisplayBottomBar = true;
            }
            else
            {
                DisplayBottomBar = false;
            }
            #endregion

            #region Debug
            /*MouseState mouse;
            mouse = Mouse.GetState();
            DebugInt = mouse.Y;
            dmg = mouse.X;*/

            if (DetectKey(Keys.Escape))
            {
                Llama += 1;
            }

            #endregion

            #region Manage Health
            EnemyHealth = GetLlamaHealth(CurrentEnemyLevel);

            if (CurrentEnemyHealth <= 0)
            {
                DebugInt = 0;
                CurrentEnemyHealth = 0;
                if (IsInBattle)
                {
                    BattleWin = true;
                }
            }

            if (CurrentPlayerHealth <= 0)
            {
                CurrentPlayerHealth = 0;
                if (IsInBattle)
                {
                    BattleLose = true;
                }
            }
            #endregion          

            previousKeyboardState = keyboardState;

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            #region Load Screens
            if (!ContentLoaded) { spriteBatch.Draw(Load, viewportRect, Color.White);
            spriteBatch.DrawString(spriteFont, disp, new Vector2(370, 125), Color.Red); }
            DrawMenu(StartTexture, 1);
            DrawMenu(HelpScreen, 3);
            DrawMenu(Credits, 5);
            DrawMenu(LlamaSelect, 6);
            DrawMenu(Story, 7);
            DrawMenu(Select2, 8);
            DrawMenu(BattleScreen, 11);
            #endregion

            #region Game Selection Screen
            DrawMenu(GameSelectScreen, 2);
            DrawSelector(Select, SelectGo, 2);
            #endregion

            #region Llama Selection Screen
            if (Screen == 6)
            {
                spriteBatch.Draw(Select, LlamaSelectGo, Color.White);

                if (LlamaSelectPos == 1)
                {
                    spriteBatch.Draw(Yellow, LlamaPos, Color.White);
                }
                if (LlamaSelectPos == 2)
                {
                    spriteBatch.Draw(Green, LlamaPos, Color.White);
                }
                if (LlamaSelectPos == 3)
                {
                    spriteBatch.Draw(White, LlamaPos, Color.White);
                }

            }
            #endregion

            #region Select2 Screen
            DrawSelector(Select, SSG, 8);
            #endregion

            #region Select2 Info
            if (Screen == 8)
            {
                if (SelectPos2 == 1)
                {
                    spriteBatch.Draw(ShopChoose, SelectInfo, Color.White);
                }
                if (SelectPos2 == 2)
                {
                    spriteBatch.Draw(InventChoose, SelectInfo, Color.White);
                }
                if (SelectPos2 == 3)
                {
                    spriteBatch.Draw(BattleChoose, SelectInfo, Color.White);
                }
                if (SelectPos2 == 4)
                {
                    spriteBatch.Draw(Save, SelectInfo, Color.White);
                }
            }
            #endregion

            #region Battle Section
            DrawSelector(BattleSelector, BSG, 11);

            if (BattleWin && Screen == 11)
            {
                spriteBatch.DrawString(spriteFont2, "WIN", new Vector2(400, 300), Color.Black);
                spriteBatch.DrawString(spriteFont2, "Press Enter to Continue",
                    new Vector2(315, 320), Color.Black);
            }

            if (BattleLose && Screen == 11)
            {
                spriteBatch.DrawString(spriteFont2, "LOSE", new Vector2(400, 300), Color.Black);
                spriteBatch.DrawString(spriteFont2, "Press Enter to Continue", new Vector2(315, 320), Color.Black);
            }

            DrawMenu(Results, 13);
            if (Screen == 13)
            {
                spriteBatch.DrawString(spriteFont2, "" + PlayerEarnedGold, GoldPos,
                    FontColor);
            }
            #endregion

            #region Shop
            DrawMenu(Shop, 9);
            DrawSelector(ShopArrow, SSSG, 9);
            #endregion

            #region Bottom Bar
            if (DisplayBottomBar)
            {
                spriteBatch.Draw(BottomBar, new Vector2(0, 585), Color.White);
                spriteBatch.DrawString(spriteFont, "Current Llama: " + GetLlamaName(Llama), new Vector2(540, 585), Color.White);
                spriteBatch.DrawString(spriteFont, "Gold: " + PlayerGold, new Vector2(30, 585), Color.White);
                spriteBatch.DrawString(spriteFont, "Mudclops Productions 2008 (c)", new Vector2(300, 585), Color.White);
            }
            #endregion

            #region Debug Shit
            spriteBatch.DrawString(spriteFont, "SelectPos2: " + SelectPos2, GSS0, Color.Red);
            spriteBatch.DrawString(spriteFont, "Screen: " + Screen, GSS1, Color.Red);
            #endregion

            #region Display Enemy and Player Information
            if (Screen == 11)
            {
                spriteBatch.DrawString(spriteFont2, "" + CurrentEnemyHealth + "/" + EnemyHealth, new Vector2(80, 15), Color.Black);
                spriteBatch.DrawString(spriteFont2, "" + CurrentPlayerHealth + "/" + PlayerHealth, new Vector2(535, 500), Color.Black);
            }
            // 105, 65
            if (Screen == 11)
            {
                spriteBatch.DrawString(spriteFont2, EnemyLlamaName, new Vector2(105, 60),
                    Color.Black);
                spriteBatch.DrawString(spriteFont2, PlayerLlamaName, new Vector2(555, 557),
                    Color.Black);
            }
            #endregion

            #region Display Llama Texture -- Battle.
            if (Screen == 11)
            {
                spriteBatch.Draw(EnemyLlamaTexture, new Vector2(540, 16), null,
                    Color.White, 0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 0);
                spriteBatch.Draw(LlamaTexture, new Vector2(20, 360), null,
                    Color.White, 0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 0);
            }
            #endregion

            spriteBatch.End();
            base.Draw(gameTime);
        }

        #region Methods
        #region Generate Damage
        public int GenerateDamage(int Damage, int EnemyLevel)
        {
            int DamageDone;
            Random rand = new Random();
            DamageDone = (int)(Damage * 0.5 + rand.Next(1, 5));

            return DamageDone;
        }
        #endregion
        #region GetLlamaDamage
        public int GetLlamaDamage(int Llama)
        {
            int Damage = 0;

            Damage = GetLlamaLevel(Llama) * (GetLlamaLevel(Llama) / 3) + 5;

            return Damage;
        }
        #endregion
        #region Run Away
        public bool RunAway()
        {
            Random rand;
            rand = new Random();

            float random = 0f;

            bool yarg = false;
            
            random = (float)rand.NextDouble();
            if (random > 0.4)
            {
                yarg = true;
            }
            else
            {
                yarg = false;
            }

            return yarg;
        }

        #endregion
        #region Draw menu
        public void DrawMenu(Texture2D Texture, int ScreenNum)
        {
            if (Screen == ScreenNum && ContentLoaded)
            {
                spriteBatch.Draw(Texture, viewportRect, Color.White);
            }
        }
        #endregion
        #region Draw Selector
        public void DrawSelector(Texture2D SelectorTexture, Vector2 Position, int ScreenNum)
        {
            if (Screen == ScreenNum && ContentLoaded)
            {
                spriteBatch.Draw(SelectorTexture, Position, Color.White);
            }
        }
        #endregion
        #region Get Llama Level
        public int GetLlamaLevel(int Llama)
        {
            /*
             * Levels
            Blue - 1
            Green - 1
            White - 1
            Grey - 2
            Red - 3
            Blue - 4
            Tiger - 5
            Rainbow - 6
            Pink - 7
            Dark Green - 8
            Zebra - 9
            Camo - 10
            Racing - 11
            Black - 12
            Dalai - 13
             */
            if (Llama == 0) { CurrentLevel = 0; }
            else if (Llama == 1 | Llama == 2 | Llama == 3) { CurrentLevel = 1; }
            else if (Llama == 4) { CurrentLevel = 2; }
            else if (Llama == 5) { CurrentLevel = 3; }
            else if (Llama == 6) { CurrentLevel = 4; }
            else if (Llama == 7) { CurrentLevel = 5; }
            else if (Llama == 8) { CurrentLevel = 6; }
            else if (Llama == 9) { CurrentLevel = 7; }
            else if (Llama == 10) { CurrentLevel = 8; }
            else if (Llama == 11) { CurrentLevel = 9; }
            else if (Llama == 12) { CurrentLevel = 10; }
            else if (Llama == 13) { CurrentLevel = 11; }
            else if (Llama == 14) { CurrentLevel = 12; }
            else if (Llama == 15) { CurrentLevel = 13; }
            else if (Llama > 15) { CurrentLevel = 1; }

            return CurrentLevel;
        }

        public int GetLlamaNumber(int LlamaLevel)
        {
            Random rand;
            rand = new Random();

            int output = 0;

            if (LlamaLevel == 0) { output = 0; }
            else if (LlamaLevel == 1) { output = (int)rand.Next(1, 3); }
            else if (LlamaLevel >= 2) { output = LlamaLevel + 2; }

            return output;
        }
        #endregion
        #region Get Llama Name
        public string GetLlamaName(int Llama)
        {
/*
Llama #s
Yellow - 1
Green - 1
White - 1
Grey - 2
Red - 3
Blue - 4
Tiger - 5
Rainbow - 6
Pink - 7
Dark Green - 8
Zebra - 9
Camo - 10
Racing - 11
Black - 12
Dalai - 13
*/
            string LlamaN;
            LlamaN = string.Empty;

            if (Llama == 0) { LlamaN = "No Llama"; }
            else if (Llama == 1) { LlamaN = "Yellow Llama"; }
            else if (Llama == 2) { LlamaN = "Green Llama"; }
            else if (Llama == 3) { LlamaN = "White Llama"; }
            else if (Llama == 4) { LlamaN = "Grey Llama"; }
            else if (Llama == 5) { LlamaN = "Red Llama"; }
            else if (Llama == 6) { LlamaN = "Blue Llama"; }
            else if (Llama == 7) { LlamaN = "Tiger Striped Llama"; }
            else if (Llama == 8) { LlamaN = "Rainbow Llama"; }
            else if (Llama == 9) { LlamaN = "Pink Llama"; }
            else if (Llama == 10) { LlamaN = "Dark Green Llama"; }
            else if (Llama == 11) { LlamaN = "Zebra Striped Llama"; }
            else if (Llama == 12) { LlamaN = "Camouflaged Llama"; }
            else if (Llama == 13) { LlamaN = "Racing Stripe Llama"; }
            else if (Llama == 14) { LlamaN = "Black Llama"; }
            else if (Llama == 15) { LlamaN = "Dalai Llama"; }
            else if (Llama > 15) { LlamaN = "Invalid Llama"; }

            return LlamaN;
        }
        #endregion
        #region Get Llama Health
        public int GetLlamaHealth(int LlamaLevel)
        {
            int LlamaHealth;

            LlamaHealth = LlamaLevel * (LlamaLevel * LlamaLevel / 5) + 20;

            if (Llama == 0 | LlamaLevel == 0)
            {
                LlamaHealth = 0;
            }

            return LlamaHealth;
        }
        #endregion
        #region Generate Enemy Level
        public int GenerateEnemyLevel(int Llama)
        {
            Random rand;
            rand = new Random();
            int LlamaLevel = 0;

            if (Llama <= 3)
            {
                LlamaLevel = rand.Next(Llama, Llama + 2);
            }
            else if (Llama >= 10)
            {
                LlamaLevel = rand.Next(Llama - 3, Llama);
            }
            else
            {
                LlamaLevel = rand.Next(Llama - 3, Llama + 3);
            }

            if (LlamaLevel >= 13)
            {
                LlamaLevel = 12;
            }

            return LlamaLevel;
        }
        #endregion
        #region Generate Gold
        public int GenerateGold(int EnemyLevel, int PlayerLevel)
        {
            int GoldGenerated;

            Random rand;
            rand = new Random();

            GoldGenerated = (Math.Abs((int)((EnemyLevel * 10 + 45) * (1 + 0.05 * (EnemyLevel - PlayerLevel)))));

            return GoldGenerated;
        }
        #endregion
        #region Load All Content
        string disp = string.Empty;

        public void LoadContent()
        {
            //Load All Screens
            disp = "Loading Screens.";
            StartTexture = content.Load<Texture2D>("Sprites\\TitleScreen");
            GameSelectScreen = content.Load<Texture2D>("Sprites\\GameSelect");
            Select = content.Load<Texture2D>("Sprites\\SelectArrow");
            HelpScreen = content.Load<Texture2D>("Sprites\\Help");
            LlamaSelect = content.Load<Texture2D>("Sprites\\LlamaSelect");
            Story = content.Load<Texture2D>("Sprites\\Story");
            Credits = content.Load<Texture2D>("Sprites\\Credits");
            Select2 = content.Load<Texture2D>("Sprites\\Select2");
            Save = content.Load<Texture2D>("Sprites\\Save");
            BattleChoose = content.Load<Texture2D>("Sprites\\BattleChoose");
            InventChoose = content.Load<Texture2D>("Sprites\\InventChoose");
            BattleScreen = content.Load<Texture2D>("Sprites\\BattleScreen");
            WinBattle1 = content.Load<Texture2D>("Sprites\\Victory_Screen");
            WinBattle2 = content.Load<Texture2D>("Sprites\\Victory_Screen2");
            ShopChoose = content.Load<Texture2D>("Sprites\\ShopChoose");
            Shop = content.Load<Texture2D>("Sprites\\shop");

            //Llamas;
            disp = "Loading Llamas.";
            Green = content.Load<Texture2D>("Sprites\\Llamas\\Green");
            White = content.Load<Texture2D>("Sprites\\Llamas\\White");
            Yellow = content.Load<Texture2D>("Sprites\\Llamas\\Yellow");
            Grey = content.Load<Texture2D>("Sprites\\Llamas\\Grey");
            Red = content.Load<Texture2D>("Sprites\\Llamas\\Red");
            Blue = content.Load<Texture2D>("Sprites\\Llamas\\Blue");
            Tiger = content.Load<Texture2D>("Sprites\\Llamas\\Tiger");
            Rainbow = content.Load<Texture2D>("Sprites\\Llamas\\Rainbow");
            Pink = content.Load<Texture2D>("Sprites\\Llamas\\Pink");
            DGreen = content.Load<Texture2D>("Sprites\\Llamas\\DGreen");
            Zebra = content.Load<Texture2D>("Sprites\\Llamas\\Zebra");
            Camo = content.Load<Texture2D>("Sprites\\Llamas\\Camo");
            Racing = content.Load<Texture2D>("Sprites\\Llamas\\Racing");
            Black = content.Load<Texture2D>("Sprites\\Llamas\\Black");
            Dalai = content.Load<Texture2D>("Sprites\\Llamas\\Dalai");


            disp = "Loading Misc!";
            BattleSelector = content.Load<Texture2D>("Sprites\\BattleSelecter");
            ShopArrow = content.Load<Texture2D>("Sprites\\ShopSelector");
            BottomBar = content.Load<Texture2D>("Sprites\\BottomBar");
            spriteFont2 = content.Load<SpriteFont>("Sprites\\Fonts\\Large");

            ContentLoaded = true;
        }

        public void ThreadLoad()
        {
            Thread t = new Thread(LoadContent);
            t.Start();
        }
        #endregion
        #region GetLlamaTexture
        public Texture2D GetLlamaTexture(int LlamaNum)
        {
            Texture2D LlamaTex;
                        
            LlamaTex = content.Load<Texture2D>("Sprites\\blank");
            if (LlamaNum == 1) { LlamaTex = Yellow; }
            if (LlamaNum == 2) { LlamaTex = Green; }
            if (LlamaNum == 3) { LlamaTex = White; }
            if (LlamaNum == 4) { LlamaTex = Grey; }
            if (LlamaNum == 5) { LlamaTex = Red; }
            if (LlamaNum == 6) { LlamaTex = Blue; }
            if (LlamaNum == 7) { LlamaTex = Tiger; }
            if (LlamaNum == 8) { LlamaTex = Rainbow; }
            if (LlamaNum == 9) { LlamaTex = Pink; }
            if (LlamaNum == 10) { LlamaTex = DGreen; }
            if (LlamaNum == 11) { LlamaTex = Zebra; }
            if (LlamaNum == 12) { LlamaTex = Camo; }
            if (LlamaNum == 13) { LlamaTex = Racing; }
            if (LlamaNum == 14) { LlamaTex = Black; }
            if (LlamaNum == 15) { LlamaTex = Dalai; }

            return LlamaTex;
        }
        #endregion

        #region Battle
        public void InitializeBattle(int Llama)
        {
            CurrentEnemyLevel = GenerateEnemyLevel(GetLlamaLevel(Llama));
            EnemyNumber = GetLlamaNumber(CurrentEnemyLevel);

            EnemyHealth = GetLlamaHealth(CurrentEnemyLevel);
            PlayerHealth = GetLlamaHealth(CurrentLevel);
            CurrentEnemyHealth = EnemyHealth;
            CurrentPlayerHealth = PlayerHealth;

            LlamaTexture = GetLlamaTexture(Llama);
            EnemyLlamaTexture = GetLlamaTexture(GetLlamaNumber(CurrentEnemyLevel));
            EnemyLlamaName = GetLlamaName(GetLlamaNumber(CurrentEnemyLevel));
            PlayerLlamaName = GetLlamaName(Llama);
        }

        public void EndBattle()
        {
            CurrentEnemyLevel = 0;
            EnemyHealth = 0;
            CurrentEnemyHealth = 0;
            BattleSelectPos = 1;
            CanAttack = false;
            RunBattleTime = true;
            IsInBattle = false;
            RunResultsTime = true;
            CanEnter = false;
            BattleWin = false;
            BattleLose = false;
            RunAITime = false;
            AIisDone = false;
            timer = 0f;
            Screen = 7;
        }
        
        public void GetBattleResults()
        {
            if (BattleWin)
            {
                Random rand;
                rand = new Random();

                Color get = new Color(0, 0, 0);
                Color get2 = new Color(255, 255, 255);

                Vector2 bs1 = new Vector2(415, 50);
                Vector2 bs2 = new Vector2(422, 69);

                int Scr = (int)rand.Next(1, 2);

                if (Scr == 1) { Results = WinBattle1; GoldPos = bs1; FontColor = get2; }
                if (Scr == 2) { Results = WinBattle2; GoldPos = bs2; FontColor = get; }

                RunResultsTime = true;

                BattleLose = false;
            }
            if (BattleLose)
            {
                int GoldLost = 0;
                GoldLost = GenerateGold(GetLlamaLevel(EnemyNumber), GetLlamaLevel(Llama)) / 2;

                if (PlayerGold < GoldLost)
                    PlayerGold -= PlayerGold;
                else
                    PlayerGold -= GoldLost;


                Screen = 7;

                EndBattle();

                BattleWin = false;
            }
        }

        public void AI(int EnemyLevel, GameTime gameTime)
        {
            RunAITime = true;
            AIisDone = false;

            //Damage = GenerateDamage(GetLlamaDamage(EnemyNumber), EnemyLevel);
            //CurrentPlayerHealth -= Damage;
        }
            
        #endregion

        #region Detection
        public bool DetectEnter()
        {
            bool Enter = false;

            if (keyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter) && ContentLoaded)
            {
                Enter = true;
            }
            else
            {
                Enter = false;
            }


            return Enter;
        }

        public bool DetectKey(Keys key)
        {
            if (keyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key) && ContentLoaded)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool DetectKey(Keys key, int Scr)
        {
            if (keyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key) && ContentLoaded)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region Save and Load
        public void LoadGame()
        {
            try
            {
                xDoc.Load("Save.xml");

                XmlNodeList LlamaID = xDoc.GetElementsByTagName("ID");
                XmlNodeList Gold = xDoc.GetElementsByTagName("Gold");

                Llama = Convert.ToInt32(LlamaID[0].InnerText);
                PlayerGold = Convert.ToInt32(Gold[0].InnerText);
                Screen = 8;

                System.Windows.Forms.MessageBox.Show("Game successfully loaded.");
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("No save found. Please start a new game.");
            }
        }

        public void SaveGame()
        {
            try
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter("Save.xml");
                sw.Write("<LlamaFarm> </LlamaFarm>");
                sw.Close();

                try
                {
                    xDoc.Load("Save.xml");
                }
                catch (System.IO.FileNotFoundException)
                {
                    XmlTextWriter xmlWrite = new XmlTextWriter("Save.xml", System.Text.Encoding.UTF8);
                    xmlWrite.Formatting = Formatting.Indented;
                    xmlWrite.WriteStartElement("LlamaFarm");
                    xmlWrite.Close();
                    xDoc.Load("Save.xml");
                }

                XmlNode root = xDoc.DocumentElement;
                XmlElement IDele = xDoc.CreateElement("ID");
                XmlElement GOLDele = xDoc.CreateElement("Gold");
                XmlElement SAVEele = xDoc.CreateElement("Save");

                XmlText IDnode = xDoc.CreateTextNode("");
                XmlText GOLDnode = xDoc.CreateTextNode("");
                XmlText SAVEnode = xDoc.CreateTextNode("");

                root.AppendChild(SAVEele);
                SAVEele.AppendChild(IDele);
                SAVEele.AppendChild(GOLDele);

                IDele.AppendChild(IDnode);
                IDnode.Value = Llama.ToString();

                GOLDele.AppendChild(GOLDnode);
                GOLDnode.Value = PlayerGold.ToString();

                xDoc.Save("Save.xml");
                System.Windows.Forms.MessageBox.Show("Save successful.");
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Save failed.");
            }
        }
        #endregion
        #endregion
    }
}